package com.ljc.service;

import com.ljc.dao.DeviceArgumentGatherDao;
import com.ljc.dao.ProjectPlanningDao;
import com.ljc.dto.DeviceArgumentResult;
import com.ljc.entity.DeviceArgumentGather;
import com.ljc.entity.ProjectPlanning;
import com.ljc.vo.ArgumentData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.Date;
import java.util.List;

@Service
public class DeviceArgumentGatherServiceImpl implements DeviceArgumentGatherService {

    @Autowired
    private DeviceArgumentGatherDao deviceArgumentGatherDao;

    @Autowired
    private ProjectPlanningDao projectPlanningDao;

    @Override
    public List<DeviceArgumentResult> getByProjectId(int projectId) {
        return deviceArgumentGatherDao.findByProjectId(projectId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void argumentConfirm(ArgumentData argumentData) {
        argumentData.setDate(new Date());
        int projectPlanningId = argumentData.getProjectPlanningId();
        deviceArgumentGatherDao.updateArgument(argumentData);
        List<DeviceArgumentResult> deviceArgumentResults = deviceArgumentGatherDao.findByProjectId(projectPlanningId);
        boolean finish = true;
        for (DeviceArgumentResult deviceArgumentResult : deviceArgumentResults) {
            if (deviceArgumentResult.getStatus() == 1) {
                finish = false;
            }
        }
        if (finish) {
            boolean pass=true;
            for (DeviceArgumentResult deviceArgumentResult : deviceArgumentResults) {
                if (deviceArgumentResult.getResult()==2){  //不通过
                    pass=false;
                }
            }
            if (pass){
                projectPlanningDao.setStatus(projectPlanningId,12);
            }else{
                projectPlanningDao.setStatus(projectPlanningId,9);
            }
        }
    }

    @Override
    public DeviceArgumentGather getOne(int id) {
        return deviceArgumentGatherDao.selectOne(id);
    }


}
